// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.commons.ui;

import org.dengues.commons.DenguesCommonsPlugin;
import org.dengues.commons.ui.jface.fieldassist.SmartField;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.dialogs.StatusDialog;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.jface.fieldassist.IContentProposalProvider;
import org.eclipse.jface.fieldassist.IControlContentAdapter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-2-29 qiang.zhang $
 * 
 */
public abstract class AbstractLoginDialog extends StatusDialog {

    /**
     * Qiang.Zhang.Adolf@gmail.com AbstractLoginDialog constructor comment.
     * 
     * @param parentShell
     */
    public AbstractLoginDialog(Shell parentShell) {
        super(parentShell);
        setShellStyle(SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | getDefaultOrientation() | SWT.RESIZE);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
     */
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setSize(400, 320);
        Rectangle screen = Display.getDefault().getBounds();
        newShell.setLocation(screen.width / 2 - 200, screen.height / 2 - 160);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "setRequiredFieldDecoration".
     * 
     * @param userText
     */
    protected void setRequiredFieldDecoration(Text userText) {
        ControlDecoration controlDecoration = new ControlDecoration(userText, SWT.CENTER | SWT.RIGHT);
        FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(
                FieldDecorationRegistry.DEC_REQUIRED);
        controlDecoration.setImage(fieldDecoration.getImage());
        controlDecoration.setDescriptionText(fieldDecoration.getDescription());
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "installContentProposalAdapter".
     * 
     * @param control
     * @param contentAdapter
     */
    protected void installContentProposalAdapter(Control control, IControlContentAdapter contentAdapter, KeyStroke keyStroke,
            boolean propagate, char[] autoActivationCharacters, int autoActivationDelay, int acceptance, int filterStyle) {
        ContentProposalAdapter adapter = new ContentProposalAdapter(control, contentAdapter, getContentProposalProvider(),
                keyStroke, autoActivationCharacters) {

            public void closeProposalPopup() {
                closeProposalPopup();
            }
        };
        adapter.setAutoActivationDelay(autoActivationDelay);
        adapter.setPropagateKeys(propagate);
        adapter.setFilterStyle(filterStyle);
        adapter.setProposalAcceptanceStyle(acceptance);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getContentProposalProvider".
     * 
     * @return
     */
    protected IContentProposalProvider getContentProposalProvider() {
        return null;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "handleFieldDecoration".
     * 
     * @param text
     */
    protected void handleFieldDecoration(Text text, final SmartField smartField) {
        text.addModifyListener(new ModifyListener() {

            public void modifyText(ModifyEvent event) {
                handleModify(smartField);
            }
        });
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "handleModify".
     * 
     * @param textField2
     */
    protected void handleModify(SmartField smartField) {
        // Error indicator supercedes all others
        if (!smartField.isValid()) {
            showError(smartField);
        } else {
            hideError(smartField);
            if (smartField.isWarning()) {
                showWarning(smartField);
            } else {
                hideWarning(smartField);
                if (smartField.hasContentAssist()) {
                    showContentAssistDecoration(smartField, true);
                } else {
                    showContentAssistDecoration(smartField, false);
                }
            }
        }
    }

    void showError(SmartField smartField) {
        FieldDecoration dec = smartField.getErrorDecoration();
        updateStatus(new Status(IStatus.ERROR, DenguesCommonsPlugin.PLUGIN_ID, 0, dec //$NON-NLS-1$
                .getDescription(), null));
        showErrorDecoration(smartField, true);
    }

    void hideError(SmartField smartField) {
        this.updateStatus(Status.OK_STATUS);
        showErrorDecoration(smartField, false);
    }

    void showWarning(SmartField smartField) {
        showWarningDecoration(smartField, true);
    }

    void hideWarning(SmartField smartField) {
        showWarningDecoration(smartField, false);
    }

    void showWarningDecoration(SmartField smartField, boolean show) {
        FieldDecoration dec = smartField.getWarningDecoration();
        ControlDecoration cd = smartField.getControlDecoration();
        if (show) {
            cd.setImage(dec.getImage());
            cd.setDescriptionText(dec.getDescription());
            cd.setShowOnlyOnFocus(false);
            cd.show();
        } else {
            cd.hide();
        }
    }

    void showErrorDecoration(SmartField smartField, boolean show) {
        FieldDecoration dec = smartField.getErrorDecoration();
        ControlDecoration cd = smartField.getControlDecoration();
        if (show) {
            cd.setImage(dec.getImage());
            cd.setDescriptionText(dec.getDescription());
            cd.setShowOnlyOnFocus(false);
            cd.show();
        } else {
            cd.hide();
        }
    }

    void showContentAssistDecoration(SmartField smartField, boolean show) {
        FieldDecoration dec = FieldDecorationRegistry.getDefault().getFieldDecoration(
                FieldDecorationRegistry.DEC_CONTENT_PROPOSAL);
        ControlDecoration cd = smartField.getControlDecoration();
        if (show) {
            cd.setImage(dec.getImage());
            cd.setDescriptionText(dec.getDescription());
            cd.setShowOnlyOnFocus(true);
            cd.show();
        } else {
            cd.hide();
        }
    }
}
